package program;

import kernel.ProcessMemory;
import kernel.exception.OSException;
import kernel.process.AbstractProcess;
import kernel.process.AbstractProcessHeader;

/**
 * Ps process.
 * For more see man() method.
 * @author Petr Hajek, Veronika Vencurikova
 *
 */
public class Ps extends AbstractProcess {
	public static final String USER_PROGRAMS = "-u";
	private boolean singleUser = false;

	public Ps(AbstractProcessHeader header, long pid) {
		super(header, pid);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String doJob(String input) throws OSException {
		String running = "";
		if (this.params.size() > 0) {
			if (this.params.get(0).toLowerCase().equals("-u")) {
				this.singleUser = true;
			}
		}
		
		if (singleUser) running = "UID\tPID\tTTY\tSTIME\tPROC\n=================================================\n" + 
				ProcessMemory.getRunningProgramsSingleUser(this.getUserID());
		else running = "UID\tPID\tTTY\tSTIME\tPROC\n=================================================\n" + 
				ProcessMemory.getRunningProgramsAll();
		return running;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean needParams() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void kill() throws OSException {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String continueJob() throws OSException {
		this.shouldEnd = true;
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String finalizeJob() throws OSException {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String man(String command) {
	String man = "";
		
		man += "    report process status\n";
		man += "\n";
		man += "Command syntax:\n";
		man += "    ps [OPTION]\n";
		man += "\n";
		man += "Description:\n";
		man += "    -u - lists running processes of current user\n";
				
		return man;
	}

}
